﻿var lastKWH = 0;
var lastAbnormalKWH = 0;
var systemTime = undefined;
var demo = undefined;
var options = undefined;
var ip = "localhost:8080/Hssy";
var lastSearchTime = undefined;
var searchTimer;
var lastIndex = [];
var second = 0;
//查询
$(document).ready(function() {
    getIP();
    setInterval(upgrade, 5 * 1000);
});
function upgrade() {
	window.control.upgrade();
}
function getIP(){
    time();
    setInterval(setTime, 1000);
    search();
}
function formatTime(time) {
    var second = parseInt(time, 10); // 秒
    var minute  = 0; // 分
    var hour = 0; // 时
    if(second > 60) {
    	minute = parseInt(second / 60, 10);
    	second = second % 60;
        if(minute > 60) {
        	hour = parseInt(minute / 60, 10);
        	minute = minute % 60;
        	return hour + "小时" + minute + "分" + second + "秒";
        } else {
        	return minute + "分" + second + "秒";
        }
    } else {
    	return second + "秒";
    }
}

function setTime() {
    $("#time").html(formatTime1(systemTime));
    if (systemTime) {
		systemTime = systemTime + 1000;
	} else {
		// time();
	}
	if(systemTime && systemTime > lastSearchTime + 30000){
		lastSearchTime = systemTime;
		clearTimeout(searchTimer);
		// search();
        $("#alert").attr("hidden", false);
	}
}

function time() {
    $.ajax({
        type : "post",
        async:false,
        cache : false,
        url : "http://" + ip + "/systemTime",
        dataType : "jsonp",
        jsonpCallback:"successTime",
        timeout : 30000,
        scriptCharset: 'utf-8',
        success: function(data) {
            systemTime = data;
            lastSearchTime = systemTime;
            setTime();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
        },
        timeout: function(data){
        }
    })
}

function search(){
	$.ajax({
	    type : "post",
  	    async: false,
  	    cache : false,
  	    url : "http://" + ip + "/andRecord",
  	    dataType : "jsonp",
  	    jsonpCallback:"successCallback",
  	    timeout : 30000,
		scriptCharset: 'utf-8',
		success: function(data){
	        lastSearchTime = systemTime;
	        $("#alert").attr("hidden", true);
            var appInfo = data.appInfo;
			var todayRecord = data.todayRecord;
			var todayStatistics = data.todayStatistics[0];
			if (todayRecord[0] == null){
                $("#totalChargingKWH").html("0");
                $("#totalChargingCount").html("0");
                $("#totalAbnormalKWH").html("0");
                $("#totalAbnormalCount").html("0");
                $("#totalTime").html("0");
                $("#maxChargingDTTM").html("0");
                $("#maxChargingKWH").html("0");
                $("#carCount").html("0");
			}else {
				var maxKWH = todayStatistics.totalChargingKWH;
				var maxDTTM = todayStatistics.totalChargingDTTM;
				var reissue = todayStatistics.resendCount;
				var carCount = todayStatistics.carCount;
				var sumTotalChargingKWH = todayStatistics.sumTotalChargingKWH;
				var sumTotalChargingDTTM = todayStatistics.sumTotalChargingDTTM;
				var totalChargingCount = todayStatistics.chargingCount;
				var sumAbnormalKWH = todayStatistics.sumAbnormalKWH || 0;
				if(!options){
	                options = {
	                    useEasing : true,
	                    useGrouping : true,
	                    separator : ',',
	                    decimal : '.',
	                    prefix : '',
	                    suffix : ''
	                };
                }
                if (sumTotalChargingKWH != lastKWH) {
	                demo = new CountUp("totalChargingKWH", lastKWH, sumTotalChargingKWH, 0, 2, options);
	                demo.start();
	                lastKWH = sumTotalChargingKWH;
	                demo = null;
                }
                if (sumAbnormalKWH != lastAbnormalKWH) {
                    demo = new CountUp("totalAbnormalKWH", lastAbnormalKWH, sumAbnormalKWH, 0, 2, options);
                    demo.start();
                    lastAbnormalKWH = sumAbnormalKWH;
                    demo = null;
                }
                $("#totalChargingCount").html(totalChargingCount);
                $("#totalAbnormalCount").html(appInfo[0].abnormalCnt);
                $("#totalTime").html(formatTime(sumTotalChargingDTTM));
                $("#maxChargingDTTM").html(formatTime(maxDTTM));
                $("#maxChargingKWH").html(maxKWH);
                $("#carCount").html(carCount);
			}
            searchRealStatus();
		},
		error: function(data){
			if (data.status == "403" && data.statusText == "Forbidden") {
			} else {
			}
			searchTimer = setTimeout("search()", 30000);
		},
		timeout: function(data){
			searchTimer = setTimeout("search()", 30000);
		}
	});
}

function searchRealStatus() {
    $.ajax({
        type : "post",
        async:false,
        cache : false,
        url : "http://" + ip + "/andStatus",
        dataType : "jsonp",
        jsonpCallback:"successCallback",
        timeout : 30000,
        scriptCharset: 'utf-8',
        success : function(data, textStatus) {
            var length = data.length;
            if(length > 5) {
                second++;
                var chargingCount = 0;
                var thisIndex = [];
                var chargingIndex = [];
                var content = '';
                for(var i = 0; i < length; i++) {
                    if(data[i].deviceState == "充电中" || data[i].communicationState == "断开" || data[i].deviceState == "故障") {
                        chargingIndex.push(i);
                    }
                    if(data[i].deviceState == "充电中") {
                        chargingCount++;
                        content = content + '<div class="eChargingStatus">' + (i+1) + '#</div>';
                    } else if(data[i].communicationState == "断开" || data[i].deviceState == "故障") {
                        content = content + '<div class="eRedStatus">' + (i+1) + '#</div>';
                    } else {
                        content = content + '<div class="eWhiteStatus">' + (i+1) + '#</div>';
                    }
                }
                $("#equipment1").empty();
                $("#equipment2").empty();
                $("#equipment3").empty();
                $("#equipment4").empty();
                $("#equipment5").empty();
                var eContent = $("#equipment5");
                eContent.html('<div class="center"><span class="eNameType">充电中/总桩 </span><span class="eNameValue2"> '+ chargingCount + '</span><span class="eNameValue">/ '+ length + '</span></div>');
                $('<div class="center">').append(content).appendTo(eContent);
                if(lastIndex.length == 0) {
                    for(var i = 0; i < (chargingIndex.length && 4); i++) {
                        thisIndex.push(chargingIndex[i]);
                    }
                    lastIndex = thisIndex;
                } else {
                    if(chargingIndex.length > 4) {
                        for(var i = 0; i < chargingIndex.length; i++) {
                            if(chargingIndex[i] > lastIndex[lastIndex.length - 1] && thisIndex.length < 4) {
                                thisIndex.push(chargingIndex[i]);
                            }
                        }
                        if(thisIndex.length < 4) {
                            for(var i = 0; i < chargingIndex.length; i++) {
                                if(thisIndex.length < 4) {
                                    thisIndex.push(chargingIndex[i]);
                                }
                            }
                        }
                    } else {
                        for(var i = 0; i < chargingIndex.length; i++) {
                            thisIndex.push(chargingIndex[i]);
                        }
                    }
                }
                if(second >= 6) {
                    second = 0;
                    if(thisIndex.length == 0) {
                       $("#equipment1").html('<div style="text-align:center;"><span class="freeStatus">空</span></div>');
                       $("#equipment2").html('<div style="text-align:center;"><span class="freeStatus">闲</span></div>');
                       $("#equipment3").html('<div style="text-align:center;"><span class="freeStatus">中</span></div>');
                       $("#equipment4").html('<div style="text-align:center;"><span class="freeStatus">...</span></div>');
                    } else {
                        for(var i = 0; i < thisIndex.length; i++) {
                            var eContent = $("#equipment" + (i + 1));
                            var index = thisIndex[i];
                            if (data[index].deviceState == "充电中") {
                                eContent.html('<div class="charging">' +
                                    '<div class="center"><span class="eNameGreen">'+ (index + 1) + '#桩</span></div>' +
                                    '<div class="center"><span class="eNameSOC">' + data[index].CurrentSOC + '%</span></div>' +
                                    '<div class="center"><span class="eNameType">自编号 </span><span class="eNameValue">' + data[index].carNumber + '</span></div>' +
                                    '<div class="center"><span class="eNameType">已充电量 </span><span class="eNameValue">' + round(data[index].chargedElectricity, 2) + '</span><span class="eNameType"> Kw·h</span></div>' +
                                    '<div class="center"><span class="eNameType">输出功率 </span><span class="eNameValue">' + round(data[index].outputVoltage * data[index].outputelectric / 1000, 2) + '</span><span class="eNameType"> kw</span></div>' +
                                    '<div class="center"><img src="../static/images/charging.png" height="70" width="70"/></div>' +
                                    '</div>');
                            } else if (data[index].deviceState == "充电结束") {
                                eContent.html('<div class="unCharging">' +
                                    '<div class="center"><span class="eNameWhite">'+ (index + 1) + '#桩</span></div>' +
                                    '<div class="center contentMargin"><span class="eNameState">待拔枪</span></div>' +
                                    '<div class="center"><img src="../static/images/after-charge.png" height="70" width="70"/></div>' +
                                    '</div>');
                            } else if (data[index].ChargingGun1 == "已连接" && data[index].deviceState != "充电中") {
                                eContent.html('<div class="unCharging">' +
                                    '<div class="center"><span class="eNameWhite">'+ (index + 1) + '#桩</span></div>' +
                                    '<div class="center contentMargin"><span class="eNameState">待拔枪</span></div>' +
                                    '<div class="center"><img src="../static/images/before-charge.png" height="70" width="70"/></div>' +
                                    '</div>');
                            } else if (data[index].communicationState == "断开" || data[index].deviceState == "故障") {
                                eContent.html('<div class="unCharging">' +
                                    '<div class="center"><span class="eNameWhite">'+ (index + 1) + '#桩</span></div>' +
                                    '<div class="center contentMargin"><span class="eNameState">故障</span></div>' +
                                    '<div class="center"><img src="../static/images/guzhang.png" height="70" width="70"/></div>' +
                                    '</div>');
                            } else {
                                eContent.html('<div class="unCharging">' +
                                    '<div class="center"><span class="eNameWhite">'+ (index + 1) + '#桩</span></div>' +
                                    '<div class="center contentMargin"><span class="eNameState">空闲</span></div>' +
                                    '<div class="center"><img src="../static/images/kongxian.png" height="70" width="70"/></div>' +
                                    '</div>');
                            }
                        }
                    }
                    lastIndex = thisIndex;
                } else {
                    for(var i = 0; i < lastIndex.length; i++) {
                        var eContent = $("#equipment" + (i + 1));
                        var index = lastIndex[i];
                        if (data[index].deviceState == "充电中") {
                            eContent.html('<div class="charging">' +
                                '<div class="center"><span class="eNameGreen">'+ (index + 1) + '#桩</span></div>' +
                                '<div class="center"><span class="eNameSOC">' + data[index].CurrentSOC + '%</span></div>' +
                                '<div class="center"><span class="eNameType">自编号 </span><span class="eNameValue">' + data[index].carNumber + '</span></div>' +
                                '<div class="center"><span class="eNameType">已充电量 </span><span class="eNameValue">' + round(data[index].chargedElectricity, 2) + '</span><span class="eNameType"> Kw·h</span></div>' +
                                '<div class="center"><span class="eNameType">输出功率 </span><span class="eNameValue">' + round(data[index].outputVoltage * data[index].outputelectric / 1000, 2) + '</span><span class="eNameType"> kw</span></div>' +
                                '<div class="center"><img src="../static/images/charging.png" height="70" width="70"/></div>' +
                                '</div>');
                        } else if (data[index].deviceState == "充电结束") {
                            eContent.html('<div class="unCharging">' +
                                '<div class="center"><span class="eNameWhite">'+ (index + 1) + '#桩</span></div>' +
                                '<div class="center contentMargin"><span class="eNameState">待拔枪</span></div>' +
                                '<div class="center"><img src="../static/images/after-charge.png" height="70" width="70"/></div>' +
                                '</div>');
                        } else if (data[index].ChargingGun1 == "已连接" && data[index].deviceState != "充电中") {
                            eContent.html('<div class="unCharging">' +
                                '<div class="center"><span class="eNameWhite">'+ (index + 1) + '#桩</span></div>' +
                                '<div class="center contentMargin"><span class="eNameState">待拔枪</span></div>' +
                                '<div class="center"><img src="../static/images/before-charge.png" height="70" width="70"/></div>' +
                                '</div>');
                        } else if (data[index].communicationState == "断开" || data[index].deviceState == "故障") {
                            eContent.html('<div class="unCharging">' +
                                '<div class="center"><span class="eNameWhite">'+ (index + 1) + '#桩</span></div>' +
                                '<div class="center contentMargin"><span class="eNameState">故障</span></div>' +
                                '<div class="center"><img src="../static/images/guzhang.png" height="70" width="70"/></div>' +
                                '</div>');
                        } else {
                            eContent.html('<div class="unCharging">' +
                                '<div class="center"><span class="eNameWhite">'+ (index + 1) + '#桩</span></div>' +
                                '<div class="center contentMargin"><span class="eNameState">空闲</span></div>' +
                                '<div class="center"><img src="../static/images/kongxian.png" height="70" width="70"/></div>' +
                                '</div>');
                        }
                    }
                }
            } else {
                for (var i = 0; i < length; i++) {
                    var eContent = $("#equipment" + (i + 1));
                    if (data[i].deviceState == "充电中") {
                        eContent.html('<div class="charging">' +
                            '<div class="center"><span class="eNameGreen">'+ (i + 1) + '#桩</span></div>' +
                            '<div class="center"><span class="eNameSOC">' + data[i].CurrentSOC + '%</span></div>' +
                            '<div class="center"><span class="eNameType">自编号 </span><span class="eNameValue">' + data[i].carNumber + '</span></div>' +
                            '<div class="center"><span class="eNameType">已充电量 </span><span class="eNameValue">' + round(data[i].chargedElectricity, 2) + '</span><span class="eNameType"> Kw·h</span></div>' +
                            '<div class="center"><span class="eNameType">输出功率 </span><span class="eNameValue">' + round(data[i].outputVoltage * data[i].outputelectric / 1000, 2) + '</span><span class="eNameType"> kw</span></div>' +
                            '<div class="center"><img src="../static/images/charging.png" height="70" width="70"/></div>' +
                            '</div>');
                    } else if (data[i].deviceState == "充电结束") {
                        eContent.html('<div class="unCharging">' +
                            '<div class="center"><span class="eNameWhite">'+ (i + 1) + '#桩</span></div>' +
                            '<div class="center contentMargin"><span class="eNameState">待拔枪</span></div>' +
                            '<div class="center"><img src="../static/images/after-charge.png" height="70" width="70"/></div>' +
                            '</div>');
                    } else if (data[i].ChargingGun1 == "已连接" && data[i].deviceState != "充电中") {
                        eContent.html('<div class="unCharging">' +
                            '<div class="center"><span class="eNameWhite">'+ (i + 1) + '#桩</span></div>' +
                            '<div class="center contentMargin"><span class="eNameState">待拔枪</span></div>' +
                            '<div class="center"><img src="../static/images/before-charge.png" height="70" width="70"/></div>' +
                            '</div>');
                    } else if (data[i].communicationState == "断开" || data.deviceState == "故障") {
                        eContent.html('<div class="unCharging">' +
                            '<div class="center"><span class="eNameWhite">'+ (i + 1) + '#桩</span></div>' +
                            '<div class="center contentMargin"><span class="eNameState">故障</span></div>' +
                            '<div class="center"><img src="../static/images/guzhang.png" height="70" width="70"/></div>' +
                            '</div>');
                    } else {
                        eContent.html('<div class="unCharging">' +
                            '<div class="center"><span class="eNameWhite">'+ (i + 1) + '#桩</span></div>' +
                            '<div class="center contentMargin"><span class="eNameState">空闲</span></div>' +
                            '<div class="center"><img src="../static/images/kongxian.png" height="70" width="70"/></div>' +
                            '</div>');
                    }
                }
            }
        },
        error: function(data){
        },
        timeout: function(data){
        }
    });
    setTimeout("search()", 1000);
}